import Vue from "vue";
import App from "./App.vue";
Vue.use(scroll);
//导入富文本
import VueQuillEditor from "vue-quill-editor";
Vue.use(VueQuillEditor);

//
import iconfont from "./css/iconfont.css";
Vue.use(iconfont);

// 引入大屏样式文件
import "@/assets/fontFamily/font.css";
import "@/assets/icons";

//导入element-ui框架
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
//重复引入 为了下面的router输出
import { Message } from "element-ui";

//导入router
import router from "./router";

//导入vuex
import store from "./vuex/store";

//导入图标库
import "font-awesome/css/font-awesome.css";
import "./assets/icon/iconfont.css";
//分辨率适配
import 'lib-flexible';
//Progress 进度条
import NProgress from "nprogress";
import "nprogress/nprogress.css";

import { filterAsyncRouter, defaultRouter } from "@/router/index";

import dataV from "@jiaminghi/data-view";

Vue.use(dataV);

Vue.use(ElementUI);
Vue.config.productionTip = false;

// //导入打印服务
import Print from "vue-print-nb";
Vue.use(Print);

import VueSuperSlide from "vue-superslide";
Vue.use(VueSuperSlide);
/**
 * 每次路由跳转前的权限验证
 */
router.beforeEach((to, from, next) => {
  NProgress.start();
  //store.commit('setBreadcrumbList', to.matched) 面包屑 暂时未打开
  if (store.getters.token) {
    if (to.path === "/login") {
      next({
        path: "/index"
      });
    } else {
      if (!store.getters.permissionRouterList) {
        store
          .dispatch("getPermissionRouterList")
          .then(() => {
            //请求后台获取权限列表
            addRouterOptionData(store.getters.permissionRouterList);
            router.addRoutes(
              filterAsyncRouter(store.getters.permissionRouterList)
            );
            next({
              path: "/screen"
            });
          })
          .catch(res => {
            sessionStorage.removeItem("token");
            Message({
              showClose: true,
              message: res,
              type: "error"
            });
            next({
              path: "/login"
            });
          });
      } else {
        next();
      }
    }
  } else {
    if (to.path === "/login") {
      next();
    }
    if (to.path === "/default") {
      next();
      NProgress.done();
      return;
    }
    next({
      path: "/login"
    });
  }
  NProgress.done();
});

//往router.option.route里面添加数据 为了左侧列表渲染
function addRouterOptionData(asyncRouterMap) {
  let defaultList = JSON.parse(JSON.stringify(defaultRouter));
  router.options.routes = [];
  for (let i = 0; i < asyncRouterMap.length; i++) {
    defaultList.push(asyncRouterMap[i]);
  }
  router.options.routes = defaultList;
}

new Vue({
  router,
  store,
  NProgress,
  render: h => h(App)
}).$mount("#app");
